# -*- coding: utf-8 -*-
import os,sys
LVSpath=/home/LVS
import MySQLdb
host='localhost' 
user='dbuser' 
passwd='dbpass' 
db='tianyalvs' 
conn = MySQLdb.connect(host,user,passwd,db,charset='utf8') 
cursor = conn.cursor()
cursor.execute("select wip,lip from ty_lvs_datacenter where state='MASTER'") 
rows = cursor.fetchall()
for lvs_row in rows:
    print "rsync rrd config"
    os.system("$LVSpath/rsync.sh "+str(lvs_row[0])+" "+str(lvs_row[1]))
    
    print "make lvs graph"
    os.system("$LVSpath/graph-lvs.sh -H tmp "+str(lvs_row[0]))
    cursor_vip = conn.cursor()
    cursor_vip.execute("select virtual_ipaddress from ty_lvs_vip where datacenter_id='"+lvs_row[0]+"'")
    rows_vip = cursor_vip.fetchall()

    for vip_row in rows_vip:
        print "make vip graph"+str(vip_row[0])
        os.system("$LVSpath/graph-lvs.sh -I "+str(vip_row[0])+" "+str(lvs_row[0]))
        cursor_server = conn.cursor()
        cursor_server.execute("select serverip from  ty_lvs_servers  where VIP='"+vip_row[0]+"'")
        rows_server = cursor_server.fetchall()
        
        for server_row in rows_server:
            print "make server graph"+str(server_row[0])
            os.system("$LVSpath/graph-lvs.sh -i "+str(server_row[0])+" "+str(lvs_row[0]))
        cursor_server.close()
    cursor_vip.close()
cursor.close()
conn.close()
